<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>商品管理</title>
    <!-- 引入css样式文件 -->
    <!-- Bootstrap Core CSS -->
    <link href="<%=basePath%>css/bootstrap.min.css" rel="stylesheet"/>
    <!-- MetisMenu CSS -->
    <link href="<%=basePath%>css/metisMenu.min.css" rel="stylesheet"/>
    <!-- DataTables CSS -->
    <link href="<%=basePath%>css/dataTables.bootstrap.css" rel="stylesheet"/>
    <!-- Custom CSS -->
    <link href="<%=basePath%>css/sb-admin-2.css" rel="stylesheet"/>
    <!-- Custom Fonts -->
    <link href="<%=basePath%>css/font-awesome.min.css" rel="stylesheet" type="text/css"/>
    <link href="<%=basePath%>css/boot-crm.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="wrapper">
    <jsp:include page="menu.jsp"/>
    <!-- 产品列表查询部分  start-->
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">商品管理</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="panel panel-default">
            <div class="panel-body">
                <form class="form-inline" method="get"
                      action="${pageContext.request.contextPath }/products/list.action">
                    <div class="form-group">
                        <label for="prodName">商品名称</label>
                        <input type="text" class="form-control" id="prodName"
                               value="${prodName }" name="prodName"/>
                    </div>
                    <div class="form-group">
                        <label for="prodPrice">商品价格</label>
                        <input type="text" class="form-control" id="prodPrice"
                               value="${prodPrice }" name="prodPrice"/>
                    </div>


                    <div class="form-group">
                        <label for="prodCategories">商品类目</label>
                        <select class="form-control" id="prodCategories" name="prodCategories">
                            <option value="">--请选择--</option>

                            <c:forEach items="${categoriesType}" var="item">
                                <option value="${item.dict_id}"
                                        <c:if test="${item.dict_id ==  prodCategories}"> selected</c:if>>
                                        ${item.dict_item_name }
                                </option>
                            </c:forEach>

                        </select>
                    </div>
                    <div class="form-group">
                        <label for="prodState">商品状态</label>
                        <select class="form-control" id="prodState" name="prodState">
                            <option value="">--请选择--</option>


                            <option value="0"
                                    <c:if test="${prodState.equals('0')}"> selected</c:if>>
                                下架
                            </option>
                            <option value="0"
                                    <c:if test="${prodState.equals('1')}"> selected</c:if>>
                                上架
                            </option>

                        </select>
                    </div>


                    <button type="submit" class="btn btn-primary">查询</button>
                </form>
            </div>
        </div>
        <a href="#" class="btn btn-primary" data-toggle="modal"
           data-target="#newProductsDialog" onclick="clearUser()">新建</a>
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">商品信息列表</div>
                    <!-- /.panel-heading -->
                    <table class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>商品id</th>
                            <th>商品名称</th>
                            <th>商品价格</th>
                            <th>商品类目</th>
                            <th>商品状态</th>
                            <th>备注</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        <c:forEach items="${page.rows}" var="row">
                            <tr>
                                <td>${row.id}</td>
                                <td>${row.prodName}</td>
                                <td>${row.prodPrice}</td>
                                <td>${row.prodCategories}</td>

                                <td>${row.prodState == 1 ? "上架":"下架"}</td>
                                <td>${row.prodRemark}</td>

                                <td>
                                    <a href="#" class="btn btn-primary btn-xs" data-toggle="modal"
                                       data-target="#userEditDialog" onclick="editCustomer(${row.id})">修改</a>
                                    <a href="#" class="btn btn-danger btn-xs" onclick="deleteCustomer(${row.id})">删除</a>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    <div class="col-md-12 text-right">
                        <ujn:page url="${pageContext.request.contextPath }/user/list.action"/>
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    </div>
    <!-- 产品列表查询部分  end-->
</div>
<!-- 创建产品模态框 -->
<div class="modal fade" id="newProductsDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">新建商品信息</h4>
            </div>
            <form class="form-horizontal" id="new_products_form">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="new_prodName" class="col-sm-2 control-label">
                            商品名称
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_prodName" placeholder="商品名称"
                                   name="prodName"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="new_prodPrice" class="col-sm-2 control-label">
                            商品价格
                        </label>
                        <div class="col-sm-10">
                            <input type="text"
                                   onkeypress="if (event.keyCode!=46 && event.keyCode!=45 && event.keyCode<48 || event.keyCode>57) event.returnValue=false"
                                   class="form-control" id="new_prodPrice" placeholder="商品价格"
                                   name="prodPrice"/>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="new_prodCategories" style="float:left;padding:7px 15px 0 27px;">商品类目</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="new_prodCategories" name="prodCategories">
                                <option value="">--请选择--</option>
                                <c:forEach items="${categoriesType}" var="item">
                                    <option value="${item.dict_id}">
                                            ${item.dict_item_name }
                                    </option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="new_prodState" style="float:left;padding:7px 15px 0 27px;">商品状态</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="new_prodState" name="prodState">
                                <option value="">--请选择--</option>
                                <option value="0">下架</option>
                                <option value="1">上架</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="new_prodRemark" class="col-sm-2 control-label">
                            备注信息
                        </label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="new_prodRemark" placeholder="备注信息"
                                   name="prodRemark"/>
                        </div>
                    </div>


                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary" onclick="createProducts()">创建商品</button>
        </div>
    </div>
</div>
<!-- 修改产品模态框 -->
<div class="modal fade" id="userEditDialog" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改商品信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="edit_user_form">
                    <input type="hidden" id="edit_ProductsId" name="id"/>
                    <div class="form-group">
                        <label for="edit_prodName" class="col-sm-2 control-label">商品名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_prodName" placeholder="商品名称"
                                   name="prodName"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_prodPrice" class="col-sm-2 control-label">商品价格</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_prodPrice" placeholder="商品价格"
                                   name="prodPrice"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="edit_prodCategories" style="float:left;padding:7px 15px 0 27px;">商品类目</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="edit_prodCategories" name="prodCategories">
                                <option value="">--请选择--</option>
                                <c:forEach items="${categoriesType}" var="item">
                                    <option value="${item.dict_id}"
                                            <c:if test="${item.dict_id == prodCategories}"> selected</c:if>>
                                            ${item.dict_item_name }
                                    </option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>


                    <div class="form-group">
                        <label for="edit_prodState" style="float:left;padding:7px 15px 0 27px;">商品状态</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="edit_prodState" name="prodState">
                                <option value="">--请选择--</option>

                                <option value="0"<c:if
                                        test="${prodState.equals('0')}"> selected</c:if>>下架
                                </option>
                                <option value="1"<c:if
                                        test="${prodState.equals('1')}"> selected</c:if>>上架
                                </option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="edit_prodRemark" class="col-sm-2 control-label">商品备注</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="edit_prodRemark" placeholder="商品备注"
                                   name="prodRemark"/>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="updateUser()">保存修改</button>
            </div>
        </div>
    </div>
</div>

<!-- 引入js文件 -->
<!-- jQuery -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.min.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>
<!-- 编写js代码 -->
<script type="text/javascript">
    //清空新建产品窗口中的数据
    function clearUser() {
        $("#new_prodName").val("");
        $("#new_prodPrice").val("")
        $("#new_prodCategories").val("")
        $("#new_prodState").val("");
        $("#new_prodRemark").val("");
    }

    // 创建
    function createProducts() {
        $.post("<%=basePath%>products/create.action",
            $("#new_products_form").serialize(), function (data) {
                if (data == "OK") {
                    alert("商品创建成功！");
                    window.location.reload();
                } else {
                    alert("商品创建失败！");
                    window.location.reload();
                }
            });
    }
    
 // 通过id获取修改的信息
    function editCustomer(id) {
        $.ajax({
            type: "get",
            url: "<%=basePath%>products/getProductsById.action",
            data: {"id": id},
            success: function (data) {
                $("#edit_ProductsId").val(data.id);
                $("#edit_prodName").val(data.prodName);
                $("#edit_prodPrice").val(data.prodPrice)
                $("#edit_prodCategories").val(data.prodCategories)
                $("#edit_prodState").val(data.prodState)
                $("#edit_prodRemark").val(data.prodRemark)
            }
        });
    }

    // 执行修改操作
    function updateUser() {
        $.post("<%=basePath%>products/update.action", $("#edit_user_form").serialize(), function (data) {
            if (data == "OK") {
                alert("商品信息更新成功！");
                window.location.reload();
            } else {
                alert("商品信息更新失败！");
                window.location.reload();
            }
        });
    }
    
    // 删除
    function deleteCustomer(id) {
        if (confirm('确实要删除该商品吗?')) {
            $.post("<%=basePath%>products/delete.action", {"id": id},
                function (data) {
                    if (data == "OK") {
                        alert("商品删除成功！");
                        window.location.reload();
                    } else {
                        alert("删除商品失败！");
                        window.location.reload();
                    }
                });
        }
    }
</script>
</body>
</html>